/// <reference path="../node_modules/chrome-extension-async/chrome-extension-async.d.ts" />
import 'chrome-extension-async'
import { notchedOutline } from 'material-components-web'

import Message from './interfaces/Message'
import { MSG_TYPE } from './interfaces/Message'

export function isNull(value: any) {
  const nullList = [NaN, null, undefined]
  return nullList.includes(value)
}

export async function getActiveTabVolumeFromStorage () {
  let volumeValue = null

  const tabId = await getActiveTabId()
  const message: Message = { name: MSG_TYPE.GET_STORAGE_VOLUME, value: 1 }
  await chrome.tabs.sendMessage(tabId, message, response => {
    volumeValue = response.value
  }) 

  return volumeValue
}

export async function getActiveTabVolume () {
  const tabId = await getActiveTabId()
  const message: Message = { name: MSG_TYPE.GET_TAB_VOLUME, tabId }
  return chrome.runtime.sendMessage(message)
}

export async function setActiveTabVolume (value: number) {
  const tabId = await getActiveTabId()
  const message: Message = { name: MSG_TYPE.SET_TAB_VOLUME, tabId, value }
  chrome.tabs.sendMessage(tabId, { name: MSG_TYPE.SET_STORAGE_VOLUME, value: value }) 
  return chrome.runtime.sendMessage(message)
}

export async function getActiveTabId () {
  const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true })
  return activeTab.id ? activeTab.id : null;
}
